﻿layui.config({
    version: true,
    base: '/Plugin/'
}).extend({ //设定模块别名
    md5: 'md5', //如果 mymod.js 是在根目录，也可以不用设定别名
    common: 'common' //相对于上述 base 目录的子目录
    }).use(['element', 'jquery', 'layer', 'table', 'form', 'common'], function () {
    var element = layui.element,
        layer = layui.layer,
        table = layui.table,
        form = layui.form,
        common = layui.common,
        $ = layui.jquery;
    //必须在框架内使用
    common.noneIframe();

    var tableRole = null;
    var tableRoleMenuIn = null;
    var tableRoleMenuOut = null;
    var tableload = {
        tablerole: function () {
            tableRole = table.render({
                elem: '#tableRole',
                url: '/Ashx/admin.ashx', //数据接口
                method: 'post',
                toolbar: "#toolbarBtn",
                where: { functionname: 'jhy_role_query' },
                cols: [[ //表头           
                    { field: 'role_name', title: '角色名称', event: "role_name", style: "cursor:pointer" },
                    {
                        field: 'role_level', title: '角色权限', event: "role_level", style: "cursor:pointer",
                        templet: function (d) {
                            var show = null;
                            if (d.role_level == '0') {
                                show = "一级";
                            } else if (d.role_level == '1') {
                                show = '二级';
                            } else if (d.role_level == '2') {
                                show = "三级";
                            } else {
                                show = "其他";
                            }
                            return show;
                        }
                    },
                    { field: 'role_menu', title: '角色菜单', event: "role_menu", style: "cursor:pointer" },
                    { title: '操作', event: "delete", templet: "<div><button class='layui-btn layui-btn-xs'>删除</button></div>", width: 80, align: "center" }
                ]]
            });
        },
        tablerolemenuout: function (role_id) {
            tableRoleMenuOut = table.render({
                elem: '#tableRoleMenuOut',
                url: '/Ashx/admin.ashx', //数据接口
                method: 'post',
                toolbar: "<div>未关联菜单</div>",
                defaultToolbar: [],
                where: { functionname: 'jhy_role_menu_query', role_id: role_id, role_field: "" },
                cols: [[ //表头   

                    { field: 'mg_name', title: '菜单组名称' },
                    { field: 'menu_name', title: '菜单名称' },
                    { title: "操作", event: "done", templet: "<div><button class='layui-btn layui-btn-xs'>选择</button></div>", width: 80, align: "center" }
                ]]
            });
        },
        tablerolemenuin: function (role_id) {
            tableRoleMenuIn = table.render({
                elem: '#tableRoleMenuIn',
                url: '/Ashx/admin.ashx', //数据接口
                method: 'post',
                toolbar: "<div>已关联菜单</div>",
                defaultToolbar: [],
                where: { functionname: 'jhy_role_menu_query', role_id: role_id, role_field: "true" },
                cols: [[ //表头                       
                    { field: 'mg_name', title: '菜单组名称' },
                    { field: 'menu_name', title: '菜单名称' },
                    { title: "操作", event: "done", templet: "<div><button class='layui-btn layui-btn-xs'>选择</button></div>", width: 80, align: "center" }
                ]]
            });
        }
    }
    tableload.tablerole();


    table.on('toolbar(tableRole)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'addrole':
                layer.open({
                    title: "添加系统角色",
                    type: 1,
                    btn: "保存",
                    skin: "jhy-layer-css",
                    content: "<div style='padding:20px 40px;'><input class='layui-input' placeholder='输入角色名称'/><br><input class='layui-input' placeholder='输入角色级别'/></div>",
                    yes: function (index, layero) {
                        var role_name = $.trim(layero.find("input:first").val());
                        var role_level = $.trim(layero.find("input:last").val());
                        var reg = /^\d+$/;
                        if (role_name == "") {
                            layer.msg("系统角色名称不能为空");
                            return false;
                        }
                        if (!reg.test(role_level)) {
                            layer.msg("系统级别输入不正确");
                            return false;
                        }
                        $.ajax({
                            type: "post",
                            url: "/Ashx/admin.ashx",
                            data: { functionname: "jhy_role_add", role_name: role_name, role_level: role_level },
                            success: function (msg) {
                                if (msg == 'true') {
                                    layer.msg("成功");
                                    tableRole.reload();
                                } else {
                                    layer.msg("失败");
                                    tableRole.reload();
                                }
                                layer.close(index);
                            }
                        });



                    }
                });
                break;
        };
    });

    table.on("tool(tableRole)", function (obj) {
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象

        if (layEvent === 'role_name') { //修改名称

            layer.prompt({
                formType: 0,
                value: data.role_name,
                title: '修改角色名称',
            }, function (value, index, elem) {
                if (data.role_name != value) {
                    $.ajax({
                        type: "post",
                        url: "/Ashx/admin.ashx",
                        data: { functionname: "jhy_role_edit", role_id: data.role_id, role_field: "role_name", role_para: value },
                        success: function (msg) {
                            if (msg == 'true') {
                                layer.msg("成功");
                                obj.update({
                                    role_name: value
                                });
                            } else {
                                layer.msg("失败");
                            }
                            layer.close(index);
                        }
                    });

                } else {
                    layer.close(index);
                }

            });

        } else if (layEvent === 'role_level') { //修改级别
            layer.prompt({
                formType: 0,
                value: data.role_level,
                title: '修改角色级别',
            }, function (value, index, elem) {
                var reg = /^\d+$/;
                if (data.role_level != value && reg.test(value) == true) {
                    $.ajax({
                        type: "post",
                        url: "/Ashx/admin.ashx",
                        data: { functionname: "jhy_role_edit", role_id: data.role_id, role_field: "role_level", role_para: value },
                        success: function (msg) {
                            if (msg == 'true') {
                                layer.msg("成功");
                                obj.update({
                                    role_level: value
                                });
                            } else {
                                layer.msg("失败");
                            }
                            layer.close(index);
                        }
                    });

                } else if (data.role_level != value && reg.test(value) == false) {
                    layer.msg("大于等于零的正整数");
                }
                else {
                    layer.close(index);
                }

            });
        } else if (layEvent === 'delete') { //删除       

            layer.confirm('真的删除' + data.role_name + '么?', { icon: 3, title: "删除角色提示" }, function (index) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/admin.ashx",
                    data: { functionname: "jhy_role_delete", role_id: data.role_id },
                    success: function (msg) {
                        if (msg == 'true') {
                            layer.msg("成功");
                            obj.del();
                        } else {
                            layer.msg("失败");
                        }
                        layer.close(index);
                    }
                });

            });

        } else if (layEvent === 'role_menu') {//修改菜单
            layer.open({
                title: "菜单管理",
                type: 1,
                skin: "jhy-layer-css",
                area: ["100%", "100%"],
                btn: "关闭窗口",
                content: $("#layerRoleMenu"),
                success: function (layero, index) {
                    tableload.tablerolemenuin(data.role_id);
                    tableload.tablerolemenuout(data.role_id);
                    $(".role_id").text(data.role_id);
                }, yes: function (index, layero) {
                    layer.close(index);
                }
            });

        }
    });

    //系统角色删除菜单
    table.on('tool(tableRoleMenuIn)', function (obj) {
        var data = obj.data;
        $.ajax({
            type: "post",
            url: "/Ashx/admin.ashx",
            data: { functionname: "jhy_role_menu_edit", role_id: data.role_id, menu_id: data.menu_id, field: "delete" },
            success: function (msg) {
                if (msg == 'true') {
                    layer.msg("成功");
                    obj.del();
                    tableRoleMenuOut.reload();
                } else {
                    layer.msg("失败");
                }
                tableRole.reload();
            }
        });

    });
    //系统角色添加菜单
    table.on('tool(tableRoleMenuOut)', function (obj) {
        var data = obj.data;
        $.ajax({
            type: "post",
            url: "/Ashx/admin.ashx",
            data: { functionname: "jhy_role_menu_edit", role_id: data.role_id, menu_id: data.menu_id, field: "add" },
            success: function (msg) {
                if (msg == 'true') {
                    layer.msg("成功");
                    tableRoleMenuIn.reload();
                    obj.del();
                } else {
                    layer.msg("失败");
                }
                tableRole.reload();
            }
        });

    });


});  
